﻿package com.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

public class DBHelp<T> {
	/**
	 * 创建dbcp，建立数据源 
	 *	@author 聂亚飞 刘海 胡志伟
	 */
	
	private static BasicDataSource bds;
	static{
		InputStream stream=DBHelp.class.getClassLoader().getResourceAsStream("db.properties");
		Properties p=new Properties();
		try {
			
			p.load(stream);	
	
			bds = new BasicDataSource();
			bds.setUrl(p.getProperty("url"));
			bds.setPassword(p.getProperty("passwd"));
			bds.setDriverClassName(p.getProperty("driver"));
			bds.setUsername(p.getProperty("name"));
			bds.setInitialSize(5);
			bds.setMaxActive(20);
			bds.setMaxWait(5000);
			bds.setMinIdle(8);
			//创建dbcp，建立数据源
			System.out.println("数据源创建完成...........................");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//创建数据库连接
	private Connection getConnection() {
		Connection conn =null;
		try {
			conn = bds.getConnection();
		}  catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 对数据库进行增加，修改
	 * @param sql
	 * @param rm
	 * @param args
	 * @return
	 */
	public List<T> executeQueryForList(String sql,RowMapper<T> rm,Object...args) {
		Connection conn = null;
		PreparedStatement stat = null;
		ResultSet rs = null;
		List<T> list = new ArrayList<T>();
		try {
			conn = getConnection();
			stat = conn.prepareStatement(sql);
			for (int i = 0; i < args.length; i++) {
				stat.setObject(i+1, args[i]);
			}
			rs = stat.executeQuery();
			while(rs.next()) {
				T obj = rm.mapRow(rs);
				list.add(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(rs, stat, conn);
		}
		return list;
	}
	
	public T executeQueryForObject(String sql,RowMapper<T> rm,Object...args) {
		Connection conn = null;
		PreparedStatement stat = null;
		ResultSet rs = null;
		T obj = null;
		try {
			conn = getConnection();
			stat = conn.prepareStatement(sql);
			for (int i = 0; i < args.length; i++) {
				stat.setObject(i+1, args[i]);
			}
			rs = stat.executeQuery();
			if(rs.next()) {
				obj = rm.mapRow(rs);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(rs, stat, conn);
		}
		return obj;
	}
	
	public boolean executeSQL(String sql,Object... args) {
		Connection conn = null;
		PreparedStatement stat = null;
		try {
			conn = getConnection();
			stat = conn.prepareStatement(sql);
			for (int i = 0; i < args.length; i++) {
				stat.setObject(i + 1, args[i]);
			}
			int rows = stat.executeUpdate();
			if(rows > 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(stat,conn);
		}
		return false;
	}
	/**
	 * 释放数据库连接
	 * @param rs
	 * @param stat
	 * @param conn
	 */
	public void close(ResultSet rs, Statement stat, Connection conn) {
		try {
			if(rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(stat != null) {
					stat.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if(conn != null) {
						conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 释放数据库连接
	 * @param rs
	 * @param stat
	 * @param conn
	 */
	public void close(Statement stat,Connection conn) {
		close(null,stat,conn);
	}
		
}
